package com.nsoft.html5npub.business;

import java.util.ArrayList;
import java.util.List;

import com.nsoft.html5npub.R;
import com.nsoft.html5npub.objects.BookRange;
import com.nsoft.html5npub.objects.Settings;
import com.nsoft.html5npub.utils.Constant;
import com.nsoft.html5npub.utils.SingleGridArrayAdapter;
import com.nsoft.html5npub.utils.engine;
import com.nsoft.html5npub.utils.utils;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Gallery;

/**
 * this class handles the synchronization with server or
 * local list at device.
 * <br>
 * the class will contact the <b>SyncBookThread</b> to 
 * perform the synchronization.
 * 
 * @author minhld
 *
 */
@SuppressWarnings("deprecation")
public class ServerGridSyncEngine implements 
			View.OnClickListener,BookSyncListener {
	
	private Context context;
	private Gallery gridGallery;
	
	public ServerGridSyncEngine(Context ctx,
						Gallery _gridGallery){
		this.context=ctx;
		this.gridGallery=_gridGallery;
	}
	
	@Override
	public void onClick(View v) {
		AlertDialog.Builder alert=
				new AlertDialog.Builder(
						this.context);
			alert.setIcon(R.drawable.ic_help);
			alert.setTitle(Constant.DIALOG_CONFIRM).
				setMessage(Constant.
						booklist.BLF_SERVER_SYNC_CONFIRM).
				setPositiveButton(Constant.DIALOG_YES,
					new DialogInterface.OnClickListener(){
						@Override
						public void onClick(
								DialogInterface arg0, 
								int arg1){
							getBookList(SyncBookThread.
									SYNC_SOURCE_SERVER);
						}
					
				}).setNegativeButton(Constant.DIALOG_NO,
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(
								DialogInterface dialog,
								int which){
							dialog.cancel();
						}
				}).create().show();
		
	}
	
	public void getBookList(int syncSrc){
		if (syncSrc==SyncBookThread.SYNC_SOURCE_SERVER){
			if (!utils.isOnline(this.context)){
				utils.showError(this.context,
					Constant.dataserver.ERROR_NETWORK_TITLE,
					Constant.dataserver.
						ERROR_NETWORK_UNAVAILABLE);
				return;
			}
		}
		
    	engine.setEngineServer(
    			Constant.dataserver.SERVER_PROTOCOL+
    			Settings.getServerName()+
    			Constant.PATH_SPLITTER);
    	String downloadUrl=Settings.getDownloadUrl();
    	if (Settings.isUseAppId()){
    		downloadUrl+=Constant.CHR_AND+
    				Constant.settingtags.APPID_PARAM+
    				Constant.CHR_EQUAL+
    				Settings.getAppId();
    	}
    	engine.setEngineParams(downloadUrl);

    	ProgressDialog pd=ProgressDialog.show(context,
				Constant.booklist.BLF_SERVER_UPDATE,
				syncSrc==SyncBookThread.SYNC_SOURCE_SERVER? 
					Constant.booklist.BLF_SERVER_SYNCHRONIZE:
					Constant.booklist.BLF_BOOKLIST_SYNCHRONIZE,
				true,false);
	
    	SyncBookThread sbt=new SyncBookThread(
    						context,pd,syncSrc);
    	sbt.setBookSyncListener(this);
    	sbt.start();
    }
	
	@Override
	public void synchronizeDone(){
		int numOfBooks=engine.getBookNumber();
		int bookPerPage=Constant.grid.
					NUM_BOOKS_ON_SINGLEGRID;
		int numOfPages=numOfBooks%bookPerPage>0?
						numOfBooks/bookPerPage+1:
						numOfBooks/bookPerPage;
		
		List<BookRange> bookRangeList=new 
						ArrayList<BookRange>();
		BookRange range;
		for (int i=0;i<numOfPages;i++){
			range=new BookRange();
			range.startIndex=i*bookPerPage;
			if ((i+1)*bookPerPage>numOfBooks){
				range.endIndex=numOfBooks-1;
			}else{
				range.endIndex=(i+1)*bookPerPage-1;
			}
			bookRangeList.add(range);
		}
		
		int[] gridSize=utils.
				getGridSizeLayout(this.context);
		
		this.gridGallery.setAdapter(new 
				SingleGridArrayAdapter(this.context,
					bookRangeList,this.gridGallery,
					gridSize));
		
	}
}
